import pymongo
import requests
import redis
import hashlib


class MgtvInfoSpider:
    url = 'https://pianku.api.mgtv.com/rider/list/pcweb/v3'

    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Origin": "https://www.mgtv.com",
        "Pragma": "no-cache",
        "Referer": "https://www.mgtv.com/",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        "sec-ch-ua-mobile": "?0"
    }

    params = {
        "allowedRC": "1",
        "platform": "pcweb",
        "channelId": "2",
        "pn": "1",
        "pc": "80",
        "hudong": "1",
        "_support": "10000000",
        "kind": "a1",
        "area": "a1",
        "year": "all",
        "feature": "all",
        "chargeInfo": "a1",
        "sort": "c2"
    }

    def __init__(self):
        self.client = pymongo.MongoClient('localhost', 27017)
        self.collection = self.client['mgtv']['tvinfo']
        self.redis_cli = redis.Redis(host='localhost', port=6379, db=0)


    @classmethod
    def get_tvinfo(cls, page_num):
        cls.params['pn'] = str(page_num)
        resp = requests.get(cls.url, headers=cls.headers, params=cls.params)
        return resp.json()


    def parse_data(self,response):
        tv = response['data']['hitDocs']
        for t in tv:
            item = dict()
            item['title'] = t['title']
            item['subtitle'] = t['subtitle']
            item['story'] = t['story']

            self.save_data(item)

    @staticmethod
    def set_md5(val):
        md5_hash = hashlib.md5(str(val).encode('utf-8')).hexdigest()

        return md5_hash

    def save_data(self,item):
        value = self.set_md5(item)
        res = self.redis_cli.sadd('tv:filter',value)
        if res:
            self.collection.insert_one(item)
            print(item,"保存成功")

    def main(self):
        for page in range(1,11):
            resp = self.get_tvinfo(page)
            self.parse_data(resp)



if __name__ == '__main__':
    m = MgtvInfoSpider()
    m.main()
